import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn } from '@delon/abc/st';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Api } from 'src/app/shared/api';
import { FileService } from 'src/app/shared/fileService';
import { ResponseCode } from 'src/app/shared/response.code';

@Component({
  selector: 'app-file',
  templateUrl: './file.component.html',
})
export class FileComponent implements OnInit {
  page:any={show:false};
  data: any[]=[];
  nodeLevel: any[] = [];
  fileType={
    folder:'folder',
    excel:'file-excel',
    word:'file-word',
    ppt:'file-ppt',
    pdf:'file-pdf',
    image:'file-image',
    gif:'file-gif',
    video:'video-camera',
    sound:'sound',
    text:'file-text',
    zip:'file-zip',
    unknown:'file-unknown'
  }
  constructor(private http: _HttpClient, private fileService: FileService,
    private msg: NzMessageService) {}

  ngOnInit() {
    this.getUserNodeByTypes();
  }

  @ViewChild('st', { static: false }) private st: STComponent;
  columns: STColumn[] = [
    { render: 'type', width:50 },
    { title: '文件名', index: 'name', sort: true},
    {
      width: 200,
      buttons: [
        {
          icon: 'download',
          iif: (record)=>this.canDownload(record),
          click: record => this.download(record),
        },
        {
          icon: 'eye',
          iif:(record)=>this.canPreview(record),
          click: record => this.preview(record),
        },
        {
          icon: 'delete',
          type:'del',
          iif:(record)=>this.canEdit(record),
          click: record => this.delete(record),
        },
      ],
    },
    { title: '大小', render: 'size' ,width: 150, sort: true},
    // { title: '拥有者', index: 'owner' ,width: 150, sort: true},
  ];

  getUserNodeByTypes(){
    let that = this;
    let params = {types:["word","ppt","excel","pdf","text"]};
    this.http.get(Api.BaseNodeApi+'/node/type',params)
      .subscribe(res=>{
        if(res.code===ResponseCode.SUCCESS){
          this.data = res.data;
        }else{
          that.msg.error(res.message?res.message:"获取失败");
        }
      });
  }

  canDownload(record):boolean{
    // TODO 判断下载权限
    return true;
  }

  canPreview(record):boolean{
    // TODO 判断预览权限
    if(record.type==='folder'){
      return false;
    }
    return true;
  }

  canEdit(record):boolean{
    // TODO 判断编辑权限
    return true;
  }

  getFileType(type):string{
    const icon = this.fileType[type];
    return icon?icon:this.fileType.unknown;
  }

  getFileSize(size):string{
    if(size){
      if(size>1024*1024*1024*1024){
        return (size/1024/1024/1024/1024).toFixed(2) +'TB';
      }else if(size>1024*1024*1024){
        return (size/1024/1024/1024).toFixed(2) +'GB';
      } else if(size>1024*1024){
        return (size/1024/1024).toFixed(2) +'MB';
      } else if(size>1024){
        return (size/1024).toFixed(2) +'KB';
      }else {
        return size+'B';
      }
    }   
  }

  rowChange(event){
    if(event&&event.type==='sort'){
      console.log('按照'+event.sort.column.indexKey+'进行'+event.sort.value+'排序');
    }
    console.log(event);
  }  

  preview(record){
    this.msg.warning('请对接预览服务');
  }

  download(record){
    this.fileService
    .download(Api.BaseNodeApi+'/download?id='+record.id)
    .subscribe(blob => {
      const a = document.createElement('a')
      const objectUrl = URL.createObjectURL(blob)
      a.href = objectUrl
      a.download = record.type==='folder'?record.name+'.zip':record.name;
      a.click();
      URL.revokeObjectURL(objectUrl);
    })
  }

  delete(record){
    let that = this;
    let params = {id:record.id}
    this.http.delete(Api.BaseNodeApi,params)
    .subscribe(res=>{
        if(res.code===ResponseCode.SUCCESS){
            this.msg.success('删除成功');
            if(this.nodeLevel.length>0){
            this.getUserNodeByTypes();
            }else{
            this.getUserNodeByTypes();
            }
        }else{
            that.msg.error(res.message?res.message:"删除失败！");
        }
    });
  }
}
